﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDDMDuocPham
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dedongia;
        private int _deid;
        private int _iidloai;
        private int _iidnhasx;
        private int _iidnhom;
        private int _istt;
        private string _sdvt;
        private string _sghichu;
        private string _shamluong;
        private string _sma;
        private string _sngaycn;
        private string _sSchema;
        private string _sten;
        private string _stenhoatchat;

        public dtoDDMDuocPham()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDDMDuocPham(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect, false);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMDuocPham.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect, bool all)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + (!all ? string.Concat(new object[] { " where  ", columnID, "=", this._deid }) : ""), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMDuocPham.cs");
                return false;
            }
        }

        public bool DeleteALL()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect, true);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "DeleteAll()", "dtoDDMDuocPham.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDDMDuocPham.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDDMDuocPham.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMDuocPham.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnMA, ",", columnTEN, ",", columnDVT, ",", columnDONGIA, ",", columnGHICHU, 
                    ",", columnSTT, ",", columnTENHOATCHAT, ",", columnHAMLUONG, ",", columnIDNHOM, ",", columnIDLOAI, ",", columnIDNHASX, ",", columnNGAYCN, ") values(", this._deid, 
                    ",'", this._sma, "',:", columnTEN, ",:", columnDVT, ",", this._dedongia, ",:", columnGHICHU, ",", this._istt, ",:", columnTENHOATCHAT, ",'", this._shamluong, 
                    "',", this._iidnhom, ",", this._iidloai, ",", this._iidnhasx, ",now())"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnDVT, NpgsqlDbType.Text, 200).Value = this._sdvt;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENHOATCHAT, NpgsqlDbType.Text, 200).Value = this._stenhoatchat;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMDuocPham.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0;
            this._sma = "";
            this._sten = "";
            this._sdvt = "";
            this._dedongia = 0M;
            this._sghichu = "";
            this._stenhoatchat = "";
            this._shamluong = "";
            this._istt = 0;
            this._iidloai = 0;
            this._iidnhom = 0;
            this._iidnhasx = 0;
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDDMDuocPham.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnMA, ",", columnTEN, ",", columnDVT, ",", columnDONGIA, ",", columnGHICHU, ",", columnSTT, ",", columnTENHOATCHAT, 
                    ",", columnHAMLUONG, ",", columnIDLOAI, ",", columnIDNHOM, ",", columnIDNHASX, " from ", this._sSchema, ".", tableName, " where ", columnID, " = ", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = int.Parse(dataTable.Rows[0][columnID].ToString());
                    this._sma = dataTable.Rows[0][columnMA].ToString();
                    this._sten = dataTable.Rows[0][columnTEN].ToString();
                    this._sdvt = dataTable.Rows[0][columnDVT].ToString();
                    this._dedongia = decimal.Parse(dataTable.Rows[0][columnDONGIA].ToString());
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                    this._istt = int.Parse(dataTable.Rows[0][columnSTT].ToString());
                    this._stenhoatchat = dataTable.Rows[0][columnTENHOATCHAT].ToString();
                    this._shamluong = dataTable.Rows[0][columnHAMLUONG].ToString();
                    this._iidloai = int.Parse(dataTable.Rows[0][columnIDLOAI].ToString());
                    this._iidnhom = int.Parse(dataTable.Rows[0][columnIDNHOM].ToString());
                    this._iidnhasx = int.Parse(dataTable.Rows[0][columnIDNHASX].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDDMDuocPham.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDDMDuocPham.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnMA + "," + columnTEN + "," + columnDVT + "," + columnDONGIA + "," + columnGHICHU + "," + columnSTT + "," + columnTENHOATCHAT + "," + columnHAMLUONG + "," + columnIDLOAI + "," + columnIDNHOM + "," + columnIDNHASX + " from " + this._sSchema + "." + tableName + " order by " + columnSTT + "," + columnID, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDDMDuocPham.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMDuocPham.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnMA, " = '", this._sma, "',", columnTEN, " = :", columnTEN, ",", columnDVT, " = :", columnDVT, 
                    ",", columnDONGIA, " = ", this._dedongia, ",", columnGHICHU, " = :", columnGHICHU, ",", columnSTT, " =", this._istt, " ,", columnTENHOATCHAT, " = :", columnTENHOATCHAT, 
                    ",", columnHAMLUONG, " = '", this._shamluong, "' ,", columnIDNHOM, "=", this._iidnhom, ",", columnIDLOAI, "=", this._iidloai, ",", columnIDNHASX, "=", this._iidnhasx, 
                    ",", columnNGAYCN, "=now() where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnDVT, NpgsqlDbType.Text, 200).Value = this._sdvt;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENHOATCHAT, NpgsqlDbType.Text, 200).Value = this._stenhoatchat;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMDuocPham.cs");
                return false;
            }
        }

        public static string columnDONGIA
        {
            get
            {
                return "dongia";
            }
        }

        public static string columnDVT
        {
            get
            {
                return "dvt";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnHAMLUONG
        {
            get
            {
                return "hamluong";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDLOAI
        {
            get
            {
                return "idloai";
            }
        }

        public static string columnIDNHASX
        {
            get
            {
                return "idnhasx";
            }
        }

        public static string columnIDNHOM
        {
            get
            {
                return "idnhom";
            }
        }

        public static string columnMA
        {
            get
            {
                return "ma";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSTT
        {
            get
            {
                return "stt";
            }
        }

        public static string columnTEN
        {
            get
            {
                return "ten";
            }
        }

        public static string columnTENHOATCHAT
        {
            get
            {
                return "tenhoatchat";
            }
        }

        private decimal DONGIA
        {
            get
            {
                return this._dedongia;
            }
            set
            {
                this._dedongia = value;
            }
        }

        public string pDVT
        {
            get
            {
                return this._sdvt;
            }
            set
            {
                this._sdvt = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public string pHAMLUONG
        {
            get
            {
                return this._shamluong;
            }
            set
            {
                this._shamluong = value;
            }
        }

        public int pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDLOAI
        {
            get
            {
                return this._iidloai;
            }
            set
            {
                this._iidloai = value;
            }
        }

        public int pIDNHASX
        {
            get
            {
                return this._iidnhasx;
            }
            set
            {
                this._iidnhasx = value;
            }
        }

        public int pIDNHOM
        {
            get
            {
                return this._iidnhom;
            }
            set
            {
                this._iidnhom = value;
            }
        }

        public string pMA
        {
            get
            {
                return this._sma;
            }
            set
            {
                this._sma = value;
            }
        }

        public string pNGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public int pSTT
        {
            get
            {
                return this._istt;
            }
            set
            {
                this._istt = value;
            }
        }

        public string pTEN
        {
            get
            {
                return this._sten;
            }
            set
            {
                this._sten = value;
            }
        }

        public string pTENHOATCHAT
        {
            get
            {
                return this._stenhoatchat;
            }
            set
            {
                this._stenhoatchat = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_dmduocpham";
            }
        }
    }
}

